#!/bin/sh
#BSUB -oo /home/zhulj/seims_phd/scenario_analysis_boundary_adaptive/SA_SLPPOS%J/SA_SLPPOS_BOUNDARY.out.%J
#BSUB -J SA_SLPPOS
#BSUB -n 48
#BSUB -R "span[ptile=1]"
#BSUB -eo /home/zhulj/seims_phd/scenario_analysis_boundary_adaptive/SA_SLPPOS%J/SA_SLPPOS_BOUNDARY.err.%J
#BSUB -q rhel6normal

source ~/.bash_profile
# Add local python installation path if needed.
export PATH=/home/zhulj/soft/python-2.7.13/bin:$PATH

# SEIMS paths
seimsroot=/home/zhulj/seims_phd
seimsprj=$seimsroot/SEIMS/seims
script=$seimsprj/scenario_analysis/spatialunits/main_nsga2.py
export PYTHONPATH=$seimsprj:$PYTHONPATH

# Job specific variables
workspace=$seimsroot/scenario_analysis_boundary_adaptive
modelpath=$seimsroot/youwuzhen10m_longterm_model
cfg=$workspace/sa_ywz10m_slppos_ibm-opt-boundary.ini
stdout=$workspace/SA_SLPPOS$LSB_JOBID/SA_SLPPOS_BOUNDARY.stdout.log.$LSB_JOBID

# Path to workspace
cd $workspace
mkdir SA_SLPPOS$LSB_JOBID
cd $workspace/SA_SLPPOS$LSB_JOBID

# start a new host file from scratch
SCOOPHOST_FILE=$workspace/SA_SLPPOS$LSB_JOBID/hosts_$LSB_JOBID
rm -f $SCOOPHOST_FILE
touch $SCOOPHOST_FILE
echo "# SCOOP hostfile created by LSF on `date`"
# check if we were able to start writing the conf file
if [ -f $SCOOPHOST_FILE ]; then
    :
else
    echo "$0: can't create $SCOOPHOST_FILE"
    exit 1
fi
HOST=""
NUM_PROC=""
FLAG=""
TOTAL_CPUS=0
echo $LSB_MCPU_HOSTS
for TOKEN in $LSB_MCPU_HOSTS
    do
        if [ -z "$FLAG" ]; then # -z means string is empty
            HOST="$TOKEN"
            FLAG="0"
        else
            NUM_PROC="$TOKEN"
            FLAG="1"
        fi
        if [ "$FLAG" = "1" ]; then
            _x=0
            if [ $_x -lt $NUM_PROC ]; then
                TOTAL_CPUS=`expr "$TOTAL_CPUS" + "$NUM_PROC"`
                echo "$HOST $NUM_PROC" >> $SCOOPHOST_FILE
            fi
            # get ready for the next host
            FLAG=""
            HOST=""
            NUM_PROC=""
        fi
    done
echo "Your SCOOP boot hostfile looks like:"
cat $SCOOPHOST_FILE

python -m scoop --hostfile $SCOOPHOST_FILE -n 48 $script -ini $cfg > $stdout
